package com.lu.sb210222client.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;

/**
 * 客户端
 */

@Slf4j
public class NettyClient implements Runnable {

    static final String HOST = System.getProperty("host", "127.0.0.1");
    static final int PORT = Integer.parseInt(System.getProperty("port", "8888"));

    private String content;

    public NettyClient(String content) {
        this.content = content;
    }

    @Override
    public void run() {
        // Configure the client.
        EventLoopGroup group = new NioEventLoopGroup();
        try {

            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .handler(new NettyClientChannelInitializer() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline p = ch.pipeline();
                            p.addLast("decoder", new StringDecoder());
                            p.addLast("encoder", new StringEncoder());
                            p.addLast(new NettyClientHandler());
                        }
                    });

            ChannelFuture future = b.connect(HOST, PORT).sync();

            future.channel().writeAndFlush(content + "--" + LocalDateTime.now());

            future.channel().closeFuture().sync();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }
    }

//    @Override
//    public  void run() {
//
//        //1.客户端定义一个循环事件组
//        EventLoopGroup group = new NioEventLoopGroup();
//
//        try {
//
//            //2.创建客户端启动对象
//            Bootstrap bootstrap = new Bootstrap();
//            bootstrap.group(group)                      //设置线程组
//                    .channel(NioSocketChannel.class)   //设置客户端通道实现类
//                    .handler(new ChannelInitializer<SocketChannel>() {
//                        @Override
//                        protected void initChannel(SocketChannel socketChannel) {
//                            socketChannel.pipeline().addLast(new NettyClientHandler());
//                        }
//                    });
//            System.err.println("client is ready......");
//
//            //3.启动客户端去连接服务端
//            ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8888).sync();
//
//            //向服务端发送消息
//            channelFuture.channel().writeAndFlush(LocalDateTime.now());
//
//            //4.设置通道关闭监听(当监听到通道关闭时，关闭client)
//            channelFuture.channel().closeFuture().sync();
//
//        } catch (InterruptedException e) {
//
//            e.printStackTrace();
//        } finally {
//
//            group.shutdownGracefully();
//        }
//    }
}
